package dproxy.application.ui.commands;

import java.util.Collection;
import java.util.Iterator;

import dproxy.application.ui.CommandLineProcessor;
import dproxy.server.cache.bo.CacheBO;
import dproxy.server.cache.dao.DatabaseImplentation;

/**
 * Removes records from persistence database.
 */
public class Delete implements Command {

    /** Sub command (command option). */
    private String subCommand;
    /** Number numeric parameter. */
    private int firstNumberParameter;
    
    /** Sub-Command that will clean up the whole cache. */
    private static final String SC_EVERYTHING = "everything";
    /** Sub-Command that will delete the last "N" entryes from cache. */
    private static final String SC_LAST_N = "last";
    /** Sub-Command that will delete record identified by "N". */
    private static final String SC_INDEX = "index";
    
    /**
     * @see dproxy.application.ui.commands.Command#description()
     */
    public String description() {
        return 
            "   'delete'     - Removes records from persistent database\n" +
            "                  - Options:\n" +
            "                    " + SC_EVERYTHING + "   - clean up the whole database\n" +
            "                    " + SC_INDEX + " N      - removes an entry with specified index\n" +
            "                    " + SC_LAST_N + " N       - remove the last N cache entryes"; 
    }
    
    /**
     * @see dproxy.application.ui.commands.Command#validate(Collection)
     */
    public void validate(Collection<String> parameters) throws ValidationException {
        
        Iterator <String> itParameters = parameters.iterator();
        if (itParameters.hasNext()) {
            subCommand = itParameters.next();
            if (SC_EVERYTHING.equals(subCommand)) {
                ValidationUtils.validateNoMoreParameters(itParameters);
            } else if(SC_LAST_N.equals(subCommand)) { 
                firstNumberParameter = ValidationUtils.getNumberParameter(itParameters);
                ValidationUtils.validateNoMoreParameters(itParameters);
            } else if(SC_INDEX.equals(subCommand)) { 
                firstNumberParameter = ValidationUtils.getNumberParameter(itParameters);
                ValidationUtils.validateNoMoreParameters(itParameters);
            }else {
                throw new ValidationException();
            }
        } else {
            throw new ValidationException();
        }
        
    }

    /**
     * @see dproxy.application.ui.commands.Command#confirm()
     */
    public boolean confirm() {
        if (SC_EVERYTHING.equals(subCommand)) {
            CommandLineProcessor.getInstance().write("WARNING: THIS WILL CLEAN UP THE WHOLE DATABASE");
            CommandLineProcessor.getInstance().write("DO YOU REALLY WANT TO DELETE EVERYTHING (Y/N)? ");
        } else if (SC_LAST_N.equals(subCommand)) {
            CommandLineProcessor.getInstance().write(
                    "Really delete the last " 
                    + firstNumberParameter
                    + " cache entries (Y/N)?");
        } else if (SC_INDEX.equals(subCommand)) {
            // No need for confirmation:
            return true;
        }
        String newInput = CommandLineProcessor.getInstance().read();
        return "Y".equalsIgnoreCase(newInput);
    }
    
    /**
     * @see dproxy.application.ui.commands.Command#execute()
     */
    public void execute() {
        DatabaseImplentation cacheImplementation
            = CommandLineProcessor.getInstance().getCacheImplementation();
        int removedEntryes = 0;
        if (SC_EVERYTHING.equals(subCommand)) {
            removedEntryes = CacheBO.getInstance(cacheImplementation).deleteEverything();
        } else if (SC_LAST_N.equals(subCommand)) {
            removedEntryes = CacheBO.getInstance(cacheImplementation).deleteLastNRows(firstNumberParameter);
        } else if (SC_INDEX.equals(subCommand)) {
            removedEntryes = CacheBO.getInstance(cacheImplementation).deleteByIndex(firstNumberParameter);
        }
        CommandLineProcessor.getInstance().write(removedEntryes + " entryes removed");
    }

}
